package com.gg.haoqi.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.gg.haoqi.BaseApp;
import com.gg.haoqi.R;
import com.gg.haoqi.adapter.VpFragAdapter;
import com.gg.haoqi.fragment.HomeFragment_;
import com.gg.haoqi.fragment.YanJiuSuoFragment_;
import com.gg.haoqi.utils.AppConstant;
import com.gg.haoqi.utils.AppManager;
import com.loopj.android.http.AsyncHttpClient;
import com.viewpagerindicator.TabPageIndicator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity {


    @ViewById(R.id.main_vpIndicator)
    TabPageIndicator vpIndicator;
    @ViewById(R.id.main_vp)
    ViewPager vp;
    @ViewById(R.id.main_drawer_cate)
    ListView cateLv;

    private long time = 0;

    private List<Fragment> fragmentList;
    private VpFragAdapter fragAdapter;
    private String titles[] = {"首页", "好奇心研究所"};

    private List<Map<String, Object>> cateList;
    private SimpleAdapter cateAdapter;
    private String[] names = {"首页", "好奇心研究所", "我的收藏", "长文章", "Top 15", "10个图", "商业", "智能", "设计", "时尚", "娱乐", "城市", "游戏", "好莱坞计划"};
    private int[] on = {R.mipmap.home, R.mipmap.yanjiu, R.mipmap.collection, R.mipmap.article, R.mipmap.top, R.mipmap.ten, R.mipmap.business, R.mipmap.intel, R.mipmap.design, R.mipmap.fashion, R.mipmap.entertain, R.mipmap.city, R.mipmap.game, R.mipmap.holly};
    private int[] nor = {R.mipmap.home_nor, R.mipmap.yanjiu_nor, R.mipmap.collection_nor, R.mipmap.article_nor, R.mipmap.top_nor, R.mipmap.ten_nor, R.mipmap.business_nor, R.mipmap.intel_nor, R.mipmap.design_nor, R.mipmap.fashion_nor, R.mipmap.entertain_nor, R.mipmap.city_nor, R.mipmap.game_nor, R.mipmap.holly_nor};

    private AsyncHttpClient client;

    @AfterViews
    public void init() {

        getSupportActionBar().hide();
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment_());
        fragmentList.add(new YanJiuSuoFragment_());
        fragAdapter = new VpFragAdapter(getSupportFragmentManager(), fragmentList, titles);
        vp.setAdapter(fragAdapter);
        vpIndicator.setViewPager(vp);

        cateList = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("img", nor[i]);
            map.put("name", names[i]);
            cateList.add(map);
        }
        cateAdapter = new SimpleAdapter(this, cateList, R.layout.item_drawer_list, new String[]{"img", "name"}, new int[]{R.id.drawer_item_icon, R.id.drawer_item_name});
        cateLv.setAdapter(cateAdapter);

        client = new AsyncHttpClient();
        initData();

        cateLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 5:
                        intent = new Intent(MainActivity.this, TenImgActivity_.class);
                        intent.putExtra("url", AppConstant.TENIMG_PATH);
                        intent.putExtra("titleName", "十张图");
                        startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(MainActivity.this, TenImgActivity_.class);
                        intent.putExtra("url", AppConstant.CWZ_PATH);
                        intent.putExtra("titleName", "长文章");
                        startActivity(intent);
                        break;
                    case 9:
                        intent = new Intent(MainActivity.this, TenImgActivity_.class);
                        intent.putExtra("url", AppConstant.FASHION_PATH);
                        intent.putExtra("titleName", "时尚");
                        startActivity(intent);
                        break;
                    case 13:
                        intent = new Intent(MainActivity.this, TenImgActivity_.class);
                        intent.putExtra("url", AppConstant.HLW_PATH);
                        intent.putExtra("titleName", "好莱坞计划");
                        startActivity(intent);
                        break;
                }

            }
        });

    }

    public void initData() {
//        TypeReference<BaseBean<Category>> typeReference = new TypeReference<BaseBean<Category>>() {
//        };
//        AsynHttpHelper.getInstance().get(AppConstant.CATE_PATH, cateList, cateAdapter, typeReference);
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - time > 1000) {
            time = currentTime;
            BaseApp.getInstance().showToast("再按一次退出程序");
        } else {
            AppManager.getAppManager().AppExit(BaseApp.getInstance());
        }
    }
}
